package com.vpx.server.service.system.impl;

import cn.hutool.core.util.XmlUtil;
import cn.hutool.json.JSONObject;
import com.vpx.server.framework.base.ResultMsg;
import com.vpx.server.service.system.SqlExtService;
import com.vpx.server.service.system.XmlProjectService;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
@Service
public class XmlProjectServiceImpl implements XmlProjectService {
    @Resource
    SqlExtService sqlExtService;
    @Override
    public void createProjectXml(JSONObject project) {
        String projectName = project.getStr("project_name");
        String projectPath = project.getStr("project_path");
        String dirPath = projectPath + File.separator + projectName;
        String projectId = project.getStr("id");
        //创建根节点
        Document document = XmlUtil.createXml();
        Element element = getProjectRootDom(document, projectId, projectName);
        document.appendChild(element);

        //创建文件夹
        File directory = new File(dirPath);
        if (!directory.exists()) {
            if (!directory.mkdirs()) {

            }
        }
        XmlUtil.toFile(document,dirPath + File.separator + projectName + ".xml");
    }


    Element getProjectRootDom(Document document,String projectId,String projectName){
        Element element = document.createElement("Project");
        element.setAttribute("version","ADE3.3.4.20230522_Release");
        element.setAttribute("NodeEnable","true");
        element.setAttribute("EXISTQUOTE","true");
        //项目名称节点
        element.appendChild(getProjectNameDom(document,projectId,projectName));
        //硬件协议
        element.appendChild(getHardFileDom(document,projectId,projectName));
        //总线协议
        element.appendChild(getBusFileDom(document,projectId,projectName));
        element.appendChild(getObserverFileDom(document,projectId,projectName));
        element.appendChild(getShareFileDom(document,projectId,projectName));
        element.appendChild(getProtocolFileDom(document,projectId,projectName));
//        element.appendChild(getFlowTemplateDom(document,projectId));
        //生成流程文件
        List<String> flowNames = this.sqlExtService.findList("select name from c_project_attr where project_id = '"+projectId+"' and config_type = '6' order by create_time asc", String.class);
        for(String flowName:flowNames) {
            element.appendChild(getDeviceDom(document,flowName));
        }
        return element;
    }

    Element getProjectNameDom(Document document,String projectId,String projectName){
        Element element = document.createElement("ProjectName");
        element.setTextContent(projectName);
        return element;
    }

    Element getHardFileDom(Document project,String projectId,String projectName){
        Element element = project.createElement("HardFile");
        element.setAttribute("NodeEnable","true");
        element.setAttribute("EXISTQUOTE","true");

        Element projectHardConfig = project.createElement("ProjectHardConfig");
        projectHardConfig.setAttribute("NodeEnable","true");
        projectHardConfig.setAttribute("EXISTQUOTE","true");
        Element fileName = project.createElement("FileName");
        fileName.setTextContent(projectName +"_Prj_HardConfig.xml");
        projectHardConfig.appendChild(fileName);
        element.appendChild(projectHardConfig);
        return element;
    }

    Element getBusFileDom(Document project,String projectId,String projectName){
        Element element = project.createElement("BusFile");
        element.setAttribute("NodeEnable","true");
        element.setAttribute("EXISTQUOTE","true");

        Element element1 = project.createElement("ProjectBusConfig");
        element1.setAttribute("NodeEnable","true");
        element1.setAttribute("EXISTQUOTE","true");
        Element element1_1 = project.createElement("FileName");
        element1_1.setTextContent(projectName+"_Prj_BusConfig.xml");
        element1.appendChild(element1_1);
        element.appendChild(element1);
        return element;
    }

    Element getObserverFileDom(Document document,String projectId,String projectName){
        Element element = document.createElement("ObserverFile");
        element.setAttribute("NodeEnable","true");
        element.setAttribute("EXISTQUOTE","true");
        Element element1 = document.createElement("FileName");
        element1.setTextContent(projectName+"_Prj_ObserverConfig.xml");
        element.appendChild(element1);
        return element;
    }

    Element getShareFileDom(Document document,String projectId,String projectName){
        Element element = document.createElement("ShareFile");
        element.setAttribute("NodeEnable","true");
        element.setAttribute("EXISTQUOTE","true");
        Element element1 = document.createElement("FileName");
        element1.setTextContent(projectName+"_Prj_ShareConfig.xml");
        element.appendChild(element1);
        return element;
    }

    Element getProtocolFileDom(Document document,String projectId,String projectName){
        Element element = document.createElement("ProtocolFile");
        element.setAttribute("NodeEnable","true");
        element.setAttribute("EXISTQUOTE","true");
        Element element1 = document.createElement("FileName");
        element1.setTextContent(projectName+"_Prj_ProtocolConfig.xml");
        Element element2 = document.createElement("ProtocolTemplate");
        element2.setAttribute("NodeEnable","true");
        element2.setAttribute("EXISTQUOTE","true");
        element.appendChild(element1);
        element.appendChild(element2);
        return element;
    }



    Element getDeviceDom(Document document,String flowName){
        Element element = document.createElement("Device");
        element.setAttribute("NodeEnable","true");
        element.setAttribute("EXISTQUOTE","true");
        Element element1 = document.createElement("DeviceName");
        element1.setAttribute("NodeEnable","true");
        element1.setAttribute("EXISTQUOTE","true");
        element1.setTextContent(flowName);
        element.appendChild(element1);
        Element element2 = document.createElement("HardConfig");
        Element element2_1 = document.createElement("FileName");
        element2.setAttribute("NodeEnable","true");
        element2.setAttribute("EXISTQUOTE","true");
        element2_1.setTextContent(flowName+"_HardCondfig.xml");
        element2.appendChild(element2_1);
        element.appendChild(element2);
        Element element3 = document.createElement("TestXML");
        element3.setAttribute("NodeEnable","true");
        element3.setAttribute("EXISTQUOTE","true");
        Element element3_1 = document.createElement("FileName");
        element3_1.setTextContent(flowName +"_Test.xml");
        element3.appendChild(element3_1);
        element.appendChild(element3);
        return element;
    }
}
